package com.xoa.controller.crm;

import com.xoa.model.crm.CrmManager;
import com.xoa.model.crm.Product;
import com.xoa.service.crm.CrmManagerService;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qiyang on 2019/7/22.
 */
@Controller
@RequestMapping("/crmManager")
public class CrmManagerController {

    @Autowired
    private CrmManagerService crmManagerService;

    /**
     * crm设置
     * @return
     */
    @RequestMapping("/index")
    public String setup() {
        return "app/crm/settingUp/index";
    }

    /**
     * 新建销售主管
     * @return
     */
    @RequestMapping("/theNewSales")
    public String theNewSales() {
        return "app/crm/settingUp/theNewSales";
    }
    @RequestMapping("/salesList")
    public String salesList() {
        return "app/crm/settingUp/salesList";
    }

    /**
     *删除权限
     * @return
     */
    @RequestMapping("/permissions")
    public String permissions() {
        return "app/crm/settingUp/permissions";
    }

    @RequestMapping("/updSales")
    public String updSales() {
        return "app/crm/settingUp/updSales";
    }


    /**
     * 设置权限
     *
     */
    @ResponseBody
    @RequestMapping("/setUpCrmManager")
    public ToJson<Integer> setUpCrmManager(HttpServletRequest request, CrmManager crmManager){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data =crmManagerService.setUpCrmManager(crmManager);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 显示设置过权限的数据
     */
    @RequestMapping("/selectByAll")
    @ResponseBody
    public ToJson<CrmManager> selectByAll(HttpServletRequest request, Integer page, Integer pageSize, Boolean useFlag,CrmManager manager){
        Map<String,Object> map= new HashMap<String, Object>();
        map.put("obj",manager);
        ToJson json =new ToJson<Product>(1,"err");
        try {
            List<CrmManager> crmManagers = crmManagerService.selectByAll(page, pageSize, useFlag, map);
            if(crmManagers!=null){
                PageParams pageParams = (PageParams) map.get("page");
                json.setTotleNum(pageParams.getTotal());
                json.setObj(crmManagers);
                json.setFlag(0);
                json.setMsg("true");
            }

        }catch(Exception e){
            json.setMsg(e.getMessage());
            json.setFlag(1);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 删除权限
     *
     */
    @ResponseBody
    @RequestMapping("/delSetUpCrmManager")
    public ToJson<Integer> delSetUpCrmManager(HttpServletRequest request, Integer mid){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data =crmManagerService.delSetUpCrmManager(mid);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 编辑回显权限
     *
     */
    @ResponseBody
    @RequestMapping("/detailSetUpCrmManager")
    public ToJson<Integer> detailSetUpCrmManager(HttpServletRequest request, Integer mid){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            CrmManager data = crmManagerService.detailSetUpCrmManager(mid);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }
    @ResponseBody
    @RequestMapping("/upSetUpCrmManager")
    public ToJson<Integer> upSetUpCrmManager(HttpServletRequest request, CrmManager crmManager){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data = crmManagerService.upSetUpCrmManager(crmManager);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }
}
